﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NFramework.DataAccess
{
    public class DataAccessContext : FrameworkContext
    {
        protected DataAccessContext() { }

        public System.Data.Common.DbConnection GetConnection()
        {
            switch (_connectionType)
            {
                case ConnectionTypes.SqlClient:
                    return new System.Data.SqlClient.SqlConnection(_connectionString);
                default:
                    return new System.Data.Odbc.OdbcConnection(_connectionString);
            }
        }

        //public System.Data.Linq.DataContext GetDataContext()
        //{
        //    return new System.Data.Linq.DataContext(_connectionString);
        //}

        private string _connectionString;
        private ConnectionTypes _connectionType;

        public static DataAccessContext PushOdbc(string connectionString)
        {
            return FrameworkContext.Push<DataAccessContext>(new DataAccessContext()
            {
                _connectionString = connectionString,
                _connectionType = ConnectionTypes.Odbc
            });
        }

        public static DataAccessContext PushSQL(string connectionString)
        {
            return FrameworkContext.Push<DataAccessContext>(new DataAccessContext() 
            { 
                _connectionString = connectionString, 
                _connectionType = ConnectionTypes.SqlClient 
            });
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]
        public static new DataAccessContext Current
        {
            get
            {
                return GetCurrent<DataAccessContext>();
            }
        }
    }
}
